# coding: utf-8
from typing import List

from PySide6.QtCore import Qt, Signal, QSize
from PySide6.QtWidgets import QWidget, QVBoxLayout, QTreeWidgetItem, QSizePolicy
from qfluentwidgets import BodyLabel, setFont, HorizontalSeparator, TransparentToolButton, FluentIcon

from components import TreeWidget


class TreeDataInterface(QWidget):
    returnSignal = Signal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self)
        self.horizontalLayout = QVBoxLayout()
        self.returnBtn = TransparentToolButton(FluentIcon.RETURN, self)
        self.titleLabel = BodyLabel('课程树数据', self)
        self.trees = []  # type: List[TreeWidget]

        setFont(self.titleLabel, 26)
        self.verticalLayout.setSpacing(20)
        self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.horizontalLayout.setAlignment(Qt.AlignmentFlag.AlignLeft)
        self.horizontalLayout.addWidget(self.returnBtn)
        self.horizontalLayout.addWidget(self.titleLabel)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.verticalLayout.addWidget(HorizontalSeparator(self))

        self.returnBtn.clicked.connect(self.returnSignal)

    def setData(self, tree_data: dict):
        data = tree_data.get('data', {})
        tid = data.get('id')
        name = data.get('name')
        self.titleLabel.setText(f'课程树数据：{name}({tid})')
        nodes = data.get('nodes', [])
        for node in nodes:
            n_id = node.get('id')
            n_name = node.get('name')
            n_nodes = node.get('nodes', [])

            label = BodyLabel(f'{n_name}({n_id})', self)
            treeWidget = TreeWidget(self)
            treeWidget.setColumnCount(3)
            treeWidget.setHeaderLabels(['名称', 'ID', '类型'])
            treeWidget.setIndentation(20)
            treeWidget.horizontalScrollMode()
            treeWidget.setColumnWidth(0, 600)
            treeWidget.setColumnWidth(1, 100)
            treeWidget.setColumnWidth(2, 100)

            treeWidget.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum)
            self.trees.append(treeWidget)

            self.verticalLayout.addWidget(label, 0, Qt.AlignmentFlag.AlignTop)
            self.verticalLayout.addWidget(treeWidget)

            for n_node in n_nodes:
                item = QTreeWidgetItem(treeWidget)
                for i in range(3):
                    item.setSizeHint(i, QSize(100, 40))
                    item.setTextAlignment(i, Qt.AlignmentFlag.AlignCenter)
                item.setTextAlignment(0, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
                item.setCheckState(0, Qt.CheckState.Unchecked)
                item.setText(0, n_node.get('name'))
                item.setText(1, n_node.get('id'))
                item.setText(2, n_node.get('type'))
                n_n_nodes = n_node.get('nodes', [])
                if not n_n_nodes:
                    continue
                for n_n_node in n_n_nodes:
                    n_item = QTreeWidgetItem(item)
                    n_item.setCheckState(0, Qt.CheckState.Unchecked)
                    for i in range(3):
                        n_item.setSizeHint(i, QSize(100, 40))
                        n_item.setTextAlignment(i, Qt.AlignmentFlag.AlignCenter)
                    n_item.setTextAlignment(0, Qt.AlignmentFlag.AlignLeft)
                    n_item.setText(0, n_n_node.get('name'))
                    n_item.setText(1, n_n_node.get('id'))
                    n_item.setText(2, n_n_node.get('type'))

        self.trees[-1].setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
